<?php
error_reporting(E_ALL);
require_once('config_fj.php');
require_once('personalInfo.php');
require_once('newSearch.php');
$link=LinkMysql();


function hasAuditPermission()//检验是否有审核权利
{
	global $browser_info;
	if($browser_info->permission=='teacher')
		return true;
	else
		return false;
	
}
//游客或者学生
function showAuditStation($title,$author,$state)
{
	//name格式:state###1###title###author###oldstate
	if( hasAuditPermission()){
	?>
	<select name="<?php echo "state###".$title.'###'.$author.'###'.$state;?>" style="width:100px">
	<option value="Y" <?php echo $state=='Y'?"selected='true'":"";?>>通过</option>
	<option value="N" <?php echo $state=='N'?"selected='true'":"";?>>未处理</option>
	<option value="D" >删除</option>
	</select>
	<?php
	}else{
		echo $state=='Y'?"通过":"未处理";
	}
}
//第一作者审核意见
function showFirAuthorOpinion($title,$author,$ostate,$permission)
{
	global $browser_info;
	if(empty($browser_info->name) || !$permission){
		echo $ostate=='Y'?"通过":"未处理";
	}else{
		?>
		<select name="<?php echo "firstAuthorOp###".$title.'###'.$author.'###'.$ostate;?>" style="width:100px" title="请仔细核对论文信息，当老师审核通过之后，论文将会移入已发表数据库">
		<option value="Y" <?php echo $ostate=='Y'?"selected='true'":"";?>>通过</option>
		<option value="N" <?php echo $ostate!='Y'?"selected='true'":"";?>>未处理</option>
		</select>
		<?php
	}
}

function sqlQuery($database,$sql,$title="")
{
	global $link;
	$ret=MysqlQuery($link,DBLABA,$sql);
	if(!$ret){
		//for debug
		if(strstr($link->error,"Duplicate")){
			?>
			<p>数据库已经存在此论文的信息，你可以<a href='<?php echo "http://10.108.107.221/lab/Searchresult.html?action=search&sortType=&rowsPerPage=&matchBoolean=true&key1=$title&value1=标题"?>'>点此</a>查看</p>
			<?php
			return false;
		}
		echo "Sql:".$sql.EOL;
		echo "Query failed at line:".__LINE__.$link->error.EOL;
		return false;
	}
	return $ret;
}

function insertPublishedPaper($cont)
{
	global $link;
	$columns=array('标题','第一作者','第二作者','第三作者','第四作者','第五作者','其他作者','通信作者','期刊会议名',
		
				   '摘要','原文语言','发表时间','页码','关键词','领域','DOI','全文下载','基金','检索号','论文类型');
	if(strstr($cont['论文类型'],'会议')){
		$columns[]='会议地点';
		$columns[]='ISBN';
	}
	else{
		$columns[]='卷';
		$columns[]='期';
		$columns[]='ISSN';
		$columns[]='CN';
	}
	$col_indexs=$values="";
	foreach($columns as $col)
	{
		if(!empty($cont[$col])){
			$cont[$col]=sqlFilter($cont[$col]);
			$col_indexs .= empty($col_indexs)?"":",";
			$col_indexs .= $col;
			$values .= empty($values)?"":",";
			$values .= "'".sqlFilter($cont[$col])."'";
		}
	}
	
	 $sql="INSERT INTO ".TBLABA." ($col_indexs) VALUES ($values)";
	// echo "INSERT:".$sql.EOL;
	return sqlQuery(DBLABA,$sql,$cont['标题']);
}

function getCertainPaperInfo($title,$author)
{
	global $link;
	$title=sqlFilter($title,true);
	$author=sqlFilter($author,true);
	$sql="SELECT * FROM ".TBIDEN." WHERE 标题 LIKE '%$title%' AND 第一作者 LIKE '%$author%' LIMIT 1";
	$ret=false;
	if(($ret=sqlQuery(DBLABA,$sql))===false){
		return 0;
	}else if(!$ret->num_rows){
		?>
		<p> 没有找到关于<font color="red"><?php echo $title;?></font>的信息</p>
		<?php
		return 0;
	}
	return $ret;
}


function dealWithPublishedPaper($title,$author,$state)
{
	global $link;
	$suc_flag="<p>已将论文<font color='red'>".$title."</font>转入已发表数据库</p>";
	$title=sqlFilter($title,true);
	$author=sqlFilter($author,true);

	
	if( $state=='Y'){
		$ret=getCertainPaperInfo($title,$author);
		if(!$ret){return 0;}
		$row=$ret->fetch_assoc();
		if($row['导师审核通过']=='Y'){
			if(insertPublishedPaper($row)){
				echo $suc_flag;
				$sql="DELETE FROM ".TBIDEN." WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
				//echo "DELETE:".$sql.EOL;
				if(!sqlQuery(DBLABA,$sql)){
					?>
					<p><font color='red'> 删除论文时发生了一些错误，请联系管理员</font></p>
					<?php
					return 0;
				}
				return 1;
			}else
			{
				return 0;
			}
		}
	} 

	$sql="UPDATE ".TBIDEN." SET 第一作者审核通过='$state' WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
	//echo "fir_sql:".$sql.EOL;
	sqlQuery(DBLABA,$sql);
	return $link->affected_rows;
 
}



function updateUnpubPaperInfo($title,$author,$state)
{
	global $link;
	$suc_flag="<p>已将论文<font color='red'>".$title."</font>转入已发表数据库</p>";
	$author=sqlFilter($author,true);
	$title=sqlFilter($title,true);
	if($state=='D'){
		$sql="DELETE FROM ".TBIDEN."  WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
	}
	switch($state){
		case 'D':
			return sqlQuery(DBLABA,$sql);
		break;
		case 'Y':
		$ret=getCertainPaperInfo($title,$author);
		if(!$ret){return 0;}	
		$row=$ret->fetch_assoc();
		if($row['第一作者审核通过']=='Y'){
			if(insertPublishedPaper($row)){
				echo $suc_flag.EOL;
				$sql="DELETE FROM ".TBIDEN." WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
				//echo "DELETE:".$sql.EOL;
				if(!sqlQuery(DBLABA,$sql)){
					?>
					<p ><font color='red'> 删除论文时发生了一些错误，请联系管理员</font></p>
					<?php
					return 0;
				}
				return $link->affected_rows;
			}else{
				return 0;
			}
		}//no break
		case 'N':
		$sql="UPDATE ".TBIDEN." SET 导师审核通过='$state' WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
		//echo "sql:".$sql.EOL;
		sqlQuery(DBLABA,$sql);
		return $link->affected_rows;
		break;
		default:break;
	}
	
	return 0;

}

function dealWithSubmit()
{
	$change_count=0;
	foreach($_POST as $name =>$selected_state)
	{
		if(strstr($name,"###")){
			$update_info=explode("###",$name);
			$title=$update_info[1];
			$author=$update_info[2];
			$old_state=$update_info[3];
			if($old_state != $selected_state){//防止处理其他变量
				$change_count+=strstr($name,"state")?updateUnpubPaperInfo($title,$author,$selected_state):dealWithPublishedPaper($title,$author,$selected_state);
			}
		}
		
	}
	if(!empty($_POST))//for debug
	echo "更新了 <font color='red'>".$change_count." </font>条记录".EOL.EOL;
}

//论文审核里，作者列的作者是通过下划线连接的
function isOneofTheAuthor($name,$row)
{
	$number=array('一','二','三','四','五');
	foreach($number as $al_nu)
	{
		$index="第".$al_nu."作者";
		if(stristr($row[$index],$name)){
			return true;
		}
	}
	
	if(stristr($row['通信作者'],$name) || stristr($row['其他作者'],$name) ){
		return true;
	}
	
	return false;
	
}

function simpleUnpubDis($row,$type)
{
	if($type=='J')
		$paper_dis_link="Ach_journal.html?title=".$row['标题']."&author=".$row['第一作者']."&type=1";
	else
		$paper_dis_link="Ach_conference.html?title=".$row['标题']."&author=".$row['第一作者']."&type=1";
	$cont="<a href='$paper_dis_link' target='_blank'>".$row['标题']."</a><br/>";
	$order=array('一','二','三','四','五','六','七','八','九','十');
	$authors="";
	for($i=0;$i<count($order);++$i)
	{
		$index="第".$order[$i]."作者";
		if(!empty($row[$index])){
			$authors .= empty($authors)?"":",";
			$authors .= $row[$index];
		}
		else{
			break;
		}
	}
	$cont .= "$authors<br/>";
	$cont .= $row['期刊会议名']."</br>";
	return $cont;
}

function disRecords($ret)
{

	global $browser_info;
	$browser_name = $browser_info->name;
	$row_index=1;
	
?>
	<ol>
<?php
		while($row=$ret->fetch_assoc())
		{
			$isFirstAuthor=false;
			if(strstr($row['第一作者'],$browser_info->name) || $browser_info->permission=='teacher'){
				$browser_info->setAudiPer(true);
				$isFirstAuthor=true;
			}
			formatPublishedPaper($row);//格式化未发表论文已发表论文共有的部分
			$type=strstr($row['论文类型'],'期刊')?"J":"C";
			$paper_id=hash('adler32',$row['标题'].$row['第一作者']);
			$cli_op="hdShowAbs('$paper_id')";
?>
			<li>
			<div><?php echo simpleUnpubDis($row,$type); $row_index++;?><a  href="javascript:void(0);" onclick="<?php echo $cli_op;?>" >摘要</a>
			<?php 
			if(isOneofTheAuthor($browser_name,$row) || $row['申请人']==$browser_name){//如果当前浏览者是论文作者之一或者是申请人
				$edit_info="author=".$row['第一作者']."&title=".$row['标题'];
				if(strstr($row['论文类型'],'期刊')){$edit_info .= "&type=-1";}
				else{$edit_info .= "&type=1";}
			?>
			<a href="unpubSubmit.php?<?php echo $edit_info;?>" target="_blank"> | 编辑</a>
			<?php
				}
			?>
			</div>
			<div id="<?php echo $paper_id;?>" style="display:none">
			<?php echo "　　".nl2br($row['摘要']);?>
			</div>
			<p>Submitted By: <?php echo $row['申请人']."　At: ".$row['申请日期'];?></p>
			<p>导师审核意见：<?php showAuditStation( $row['标题'],$row['第一作者'],$row['导师审核通过']);?>　　
　　　　　　　	<?php if($row['当前状态']=="已发表"){
					$auditState=$row['第一作者审核通过'];
				?>
				第一作者审核意见：<?php showFirAuthorOpinion($row['标题'],$row['第一作者'],$auditState,$isFirstAuthor);?>
				<?php
					}
				?>
			</p>
			</li>
			<?php
		}
			?>
	</ol>
<?php

}
//录用待发表、大修、小修、被拒、已提交、待审核
function getStates($state)
{
	global $link;
	$sql="SELECT * FROM ".TBIDEN." WHERE 当前状态 LIKE '%$state%' ORDER BY 申请日期 ASC";
	$ret=MysqlQuery($link,DBLABA,$sql);
	if(!$ret){
		//for debug
		echo "Sql:".$sql.EOL;
		echo "Query failed at line:".__LINE__.".".$link->error.EOL;
	}
	else if($ret->num_rows){
		echo "<b>$state</b>:<br/>";
		disRecords($ret);
	}
}

function submitPermission()
{
	global $browser_info;
	return hasAuditPermission() || $browser_info->audit_permission;	
}


//显示论文
function disAll()
{
	
	$states=array('录用待发表','大修','小修','被拒','已提交','待审核','已发表');
	?>
	<form action="" method="post">
	<?php
	foreach($states as $val)
	{
		getStates($val);
	}
	if( submitPermission()){
		?>
		<div style="margin-left:560px">
		<input type="submit" value="保存更改"><br/><br/>
		</div
		<?php
	}
	?>
	</form>
	<?php	
}
	
?>
